<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/13/013
 * Time: 18:10
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\MemberBill as MemberBillModel;
use app\api\model\Bank as BankModel;
use app\api\model\MemberBank as MemberBankModel;
use app\api\model\MemberWithdraw as MemberWithdrawModel;
use app\api\model\Member as MemberModel;
use app\api\model\OrderRecharge as OrderRechargeModel;
use app\api\service\Token;
use app\api\validate\BankValidate;
use app\api\validate\ClothesMoneyValidate;
use app\api\validate\IDMustBePostiveInt;
use app\api\validate\OrderValidate;
use app\api\validate\WithdrawValidate;
use app\lib\exception\SuccessMessage;

class Wallet extends BaseController {

    /**
     * 获取账单列表
     * @return array
     */
    public function detailList() {
        $data = MemberBillModel::detailList();
        return $data;
    }

    /**
     * 获取账单详情
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     */
    public function detailInfo($id) {
        (new IDMustBePostiveInt())->goCheck();
        $data = MemberBillModel::detailInfo($id);
        return $data;
    }

    /**
     * 获取银行列表
     * @return array
     */
    public function bankList() {

        $bank = BankModel::all();
        return $bank;
    }

    /**
     * 获取用户银行卡列表
     * @return array
     */
    public function memberBankList() {

        $uid = Token::getCurrentUid();
        $bank = MemberBankModel::all(['member_id' => $uid], 'bank');

        return $bank;
    }

    /**
     * 添加或者修改用户银行卡信息
     * @return \think\response\Json
     */
    public function createAndUpdateMemberBank() {
        $bankValidate = new BankValidate();
        $bankValidate->goCheck();
        $params = $bankValidate->getDataByRule(input('post.'));
        $id = 0;
        if (input('?post.id')) {
            (new IDMustBePostiveInt())->goCheck();
            $id = input('post.id');
        }
        MemberBankModel::createAndUpdateMemberBank($params, $id);

        return json(new SuccessMessage(), 201);
    }

    /**
     * 申请提现
     * @return \think\response\Json
     */
    public function applyWithdraw() {
        $withdrawValidate = new WithdrawValidate();
        $withdrawValidate->goCheck('base_withdraw');
        $params = $withdrawValidate->getDataByRule(input('post.'));
        if ($params['account_to_be_credited_type'] == 1) {
            $withdrawValidate->goCheck('member_bank_id');
            $params['member_bank_id'] = input('post.member_bank_id');
        }

        MemberWithdrawModel::applyWithdraw($params);

        return json(new SuccessMessage(), 201);
    }

    /**
     * 赠送衣币给好友
     * @return \think\response\Json
     */
    public function giveClothesMoney() {
        (new ClothesMoneyValidate())->goCheck();
        $username = input('param.username');
        $clothes_money = input('param.clothes_money');
        MemberModel::giveClothesMoney($username, $clothes_money);
        return json(new SuccessMessage(), 201);
    }

    /**
     * 创建充值订单
     * @return array
     */
    public function createRecharge() {
        $orderValidate = new OrderValidate();
        $orderValidate->goCheck('recharge');
        $params = $orderValidate->getDataByRule(input('param.'));
        return OrderRechargeModel::createRecharge($params)->toArray();
    }
}